/**
 * Author: w景洋.
 * Time: 2016/4/19 0019
 */
angular.module('service.shop', [
        "ngResource"
    ])
    //获取商店列表
    .service('ShopService', ['$resource', function ($resource) {
        var shop_map = {};

        //获取县镇商店菜单
        this.selectAllCountyShops = function (countyid) {
            var Shoplist = $resource(mallurl + 'shoplist/:id', {}, {
                get: {cache: true, method: 'GET'},
                query: {cache: true, method: 'GET', isArray: true}
            });
            return Shoplist.query({id: countyid}, function (data) {
                for (var i = 0; i < data.length; i++) {
                    var town_shops = data[i];
                    for (var j = 0; j < town_shops.shops.length; j++) {
                        var shop = town_shops.shops[j];
                        shop_map[shop.shopid] = shop;
                    }
                }
            });
        }

        //获取商店(优先从缓存读取)
        this.queryShopByShopid = function (shopid) {
            if (shop_map[shopid]) {
                return shop_map[shopid];
            }
            var shopres = $resource(mallurl + 'shops/:id', {}, {
                get: {cache: true, method: 'GET'}
            });
            return shopres.get({id: shopid});
        }

        //获取分组商店菜单
        this.queryShoplistByGroup = function (groupid) {
            var ShopByGroup = $resource(mallurl + 'shoplist/bytag/:id', {}, {
                get: {cache: true, method: 'GET'},
                query: {cache: true, method: 'GET', isArray: true}
            });
            return ShopByGroup.query({id: groupid});
        }
    }])
;
